package com.fh.wanandroid.ui.wx.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.fh.wanandroid.R;
import com.fh.wanandroid.app.Constants;
import com.fh.wanandroid.base.BaseFragment;
import com.fh.wanandroid.base.BaseResponse;
import com.fh.wanandroid.ui.wx.bean.WXAuthorListData;
import com.fh.wanandroid.ui.wx.contract.WXArticleContract;
import com.fh.wanandroid.ui.wx.presenter.WXAuthorPresenter;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class WXArticleFragment extends BaseFragment<WXArticleContract.Presenter> implements WXArticleContract.View {

    @BindView(R.id.wx_article_tab_layout)
    SlidingTabLayout wxArticleTabLayout;

    @BindView(R.id.wx_article_viewpager)
    ViewPager wxArticleViewpager;


    List<WXDetailArticlelFragment> wxDetailArticlelFragments;


    BaseResponse<List<WXAuthorListData>> wxAuthorListData;

    @Override
    public WXArticleContract.Presenter getmPresenter() {
        return new WXAuthorPresenter();
    }

    @Override
    public void presenterSetView() {

        if (mPresenter != null) {
            mPresenter.setView(this);
        }

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_wx_article;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

        mPresenter.getWxAuthorListData();
        wxDetailArticlelFragments = new ArrayList<>();

    }

    @Override
    public void loadData() {

    }

    @Override
    public void showWXAutor(BaseResponse<List<WXAuthorListData>> wxAuthorListData) {


        this.wxAuthorListData = wxAuthorListData;

        for (WXAuthorListData data : wxAuthorListData.getData()) {


            wxDetailArticlelFragments.add(WXDetailArticlelFragment.getInstance(data.getName(),  data.getId()));
        }

        WxArticleVpAdaper wxArticleVpAdaper = new WxArticleVpAdaper(getChildFragmentManager(), getContext());

        wxArticleViewpager.setAdapter(wxArticleVpAdaper);

        wxArticleTabLayout.setViewPager(wxArticleViewpager);

    }

    @Override
    public void loadDataFail(String msg) {

    }


    public static WXArticleFragment getInstance(String param1, String param2) {

        WXArticleFragment fragment = new WXArticleFragment();
        Bundle args = new Bundle();
        args.putString(Constants.ARG_PARAM1, param1);
        args.putString(Constants.ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    public class WxArticleVpAdaper extends FragmentPagerAdapter {

        Context context;

        public WxArticleVpAdaper(FragmentManager fm, Context context) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {

            return wxDetailArticlelFragments.get(position);
        }

        @Override
        public int getCount() {

            return wxDetailArticlelFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return wxAuthorListData.getData().get(position).getName();
        }
    }

}
